﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RabbitMQ.Client;

namespace JWUX.Supermarket.MQL
{
    public class BSConnection
    {
        /// <summary>
        /// 连接RabbitMQ服务器
        /// </summary>
        /// <param name="hostName">服务器名</param>
        /// <param name="userName">指定用户名</param>
        /// <param name="Password"></param>
        /// <param name="Port">指定一个端口号</param>
        /// <returns></returns>
        public static IConnection GetConnection(string hostName,
            string userName = ConnectionFactory.DefaultUser,
            string Password = ConnectionFactory.DefaultPass,
            int Port = AmqpTcpEndpoint.UseDefaultPort)
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = hostName;
            factory.UserName = userName;
            factory.Password = Password;
            factory.Protocol = Protocols.DefaultProtocol;
            factory.Port = Port;

            IConnection conn = factory.CreateConnection();
            return conn;
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <param name="conn"></param>
        public void CloseConnection(IConnection conn)
        {
            conn.Close();
        }
    }
}
